package com.educate.common.utils.converter;

import com.educate.common.utils.action.Func1;
import org.springframework.core.convert.converter.Converter;

import java.util.ArrayList;
import java.util.List;

public class ListUtils {

    public static <S, T> List<T> map(List<S> source, Converter<S, T> transform) {
        if (source == null) return null;
        List<T> target = new ArrayList<>(source.size());
        for (S s : source) {
            target.add(transform.convert(s));
        }
        return target;
    }

    public static <S, T> List<T> map(List<S> source, Converter<S, T> transform , Func1<Boolean, T> filter) {
        if (source == null) return null;
        List<T> target = new ArrayList<>(source.size());
        for (S s : source) {
            T convert = transform.convert(s);
            Boolean isFilter = filter.call(convert);
            if (!isFilter) target.add(convert);
        }
        return target;
    }

}
